<template>
  <el-card>
    <el-table border :data="skuArr">
      <el-table-column
        label="序号"
        align="center"
        width="80px"
        type="index"
      ></el-table-column>
      <el-table-column label="名称" prop="skuName"></el-table-column>
      <el-table-column label="描述" prop="skuDesc"></el-table-column>
      <el-table-column label="图片">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column label="价格(元)" prop="price"></el-table-column>
      <el-table-column label="重量(g)" prop="weight"></el-table-column>
      <el-table-column label="操作" width="360px">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale == 0 ? 'Top' : 'Bottom'"
            @click="isSale(row.isSale, row.id)"
          >
            {{ row.isSale == 0 ? '上架' : '下架' }}
          </el-button>
          <el-button type="primary" size="small" icon="Edit" @click="update">
            编辑
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="info(row.id)"
          >
            详情
          </el-button>
          <el-popconfirm
            :title="`您确定删除 ${row.skuName} 吗`"
            @confirm="del(row.id)"
            width="200px"
          >
            <template #reference>
              <el-button type="primary" size="small" icon="Delete">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      style="margin: 15px 0px"
      v-model:current-page="page"
      v-model:page-size="limit"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @size-change="getSkuList()"
      @current-change="handleCurrentChange"
    />

    <!-- 抽屉 -->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>sku详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">sku名称</el-col>
          <el-col :span="16">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">sku描述</el-col>
          <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">sku价格(元)</el-col>
          <el-col :span="16">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">sku重量(g)</el-col>
          <el-col :span="16">{{ skuInfo.weight }}</el-col>
        </el-row>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">平台属性</el-col>
          <el-col :span="16">
            <el-tag
              v-for="(item, index) in skuInfo.skuAttrValueList"
              :key="index"
              style="margin: 2px 5px"
            >
              {{ item.attrName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">销售属性</el-col>
          <el-col :span="16">
            <el-tag
              v-for="(item, index) in skuInfo.skuSaleAttrValueList"
              :key="index"
              style="margin: 2px 5px"
            >
              {{ item.saleAttrName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="margin: 20px 0px">
          <el-col :span="8">图片</el-col>
          <el-col :span="16">
            <el-carousel type="card" height="200px">
              <el-carousel-item
                interval="3000"
                v-for="item in skuInfo.skuImageList"
                :key="item"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>
<script setup lang="ts">
import { ElMessage } from 'element-plus'
import {
  reqSkuList,
  reqCancelSale,
  reqOnSale,
  reqSkuInfo,
  reqDeleteSku,
} from '@/api/pro/sku'
import { onMounted, ref } from 'vue'

let page = ref(1)
let limit = ref(10)
let total = ref(10)
let skuArr = ref([])
let drawer = ref(false)
let skuInfo = ref({})

onMounted(() => {
  getSkuList()
})
//sku列表
const getSkuList = async (pageSize = 1) => {
  page.value = pageSize
  let res = await reqSkuList(page.value, limit.value)
  skuArr.value = res.data.records
  total.value = res.data.total
}
//页面改变
const handleCurrentChange = () => {
  getSkuList(page.value)
}
//上架下架
const isSale = async (val, skuId) => {
  if (val == 0) {
    await reqOnSale(skuId)
    ElMessage({ type: 'success', message: '上架成功' })
  } else {
    await reqCancelSale(skuId)
    ElMessage({ type: 'success', message: '下架成功' })
  }
  //刷新页面
  getSkuList(page.value)
}
//编辑sku
const update = () => {
  ElMessage('编辑sku还未完成，正在努力更新中...')
}
//删除sku
const del = async (skuId) => {
  let res = await reqDeleteSku(skuId)
  if (res.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    //刷新页面
    getSkuList(skuArr.value.length >= 1 ? page.value : page.value - 1)
  } else {
    ElMessage({ type: 'error', message: '删除失败' })
  }
}
//详情
const info = async (spuId) => {
  let res = await reqSkuInfo(spuId)
  skuInfo.value = res.data
  drawer.value = true
}
</script>
<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
